package kingwing.dc.core.sevice;

import java.util.List;



import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;

import kingwing.dc.core.model.Infos;
import kingwing.dc.core.model.Rule;
import kingwing.dc.core.model.RuleDetail;
import kingwing.dc.core.orm.hibernate.CommonHibernateDao;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * 列表+详细页面采集处理类（静态页面处理）
 * @author Kevin
 * 2017年10月27日
 */
public class HTMLProcessor implements PageProcessor {
	

    /**
     * 网站配置类
     */
    private Site site;
    
    /*
     * 采集规则
     */
    private Rule rule;
    
    /**
     * 采集规则明细-采集字段
     */
    private List<RuleDetail> ruleDetails;
	
	public HTMLProcessor(Site site, Rule rule, List<RuleDetail> ruleDetails) {
		super();
		this.site = site;
		this.rule = rule;
		this.ruleDetails = ruleDetails;
	}


	@Override
	public void process(Page page) {
		//列表页
		if (page.getUrl().regex(rule.getHelperUrlRegexes()).match()) {
			page.addTargetRequests(page.getHtml().links().regex(rule.getContentUrlRegexes()).all());
		
		//文章页
		} 
		if(page.getUrl().regex(rule.getContentUrlRegexes()).match()){
			Infos infor = new Infos();
			infor.setRuleName(rule.getRuleName());
			String flag = "用处：判断采集字段是否为空；空则跳过存储，反之存储结果";
			rule:for (RuleDetail ruleDetail : ruleDetails) {
				int feildType = ruleDetail.getFieldType();
				switch (feildType) {
				case 0:
					String field1 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
					if (StringUtils.isBlank(field1)) {
						page.setSkip(true);
						flag=field1;
						break rule;
					}
					infor.setField1(field1.trim());
					break;
				case 1:
					String field2 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
					if (StringUtils.isBlank(field2)) {
						page.setSkip(true);
						flag=field2;
						break rule;
					}
					infor.setField2(field2.trim());
					break;
				case 2:
					String field3 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
					if (StringUtils.isBlank(field3)) {
						page.setSkip(true);
						flag=field3;
						break rule;
					}
					field3 = formatToDate(field3.trim());
					infor.setField3(field3);
					break;
				case 3:
					String field4 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
					if (StringUtils.isBlank(field4)) {
						page.setSkip(true);
						flag=field4;
						break rule;
					}
					infor.setField4(field4.trim());
					break;
				case 4:
					String field5 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
					if (StringUtils.isBlank(field5)) {
						page.setSkip(true);
						flag=field5;
						break rule;
					}
					infor.setField5(field5.trim());
					break;
				case 5:
					String field6 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
					if (StringUtils.isBlank(field6)) {
						page.setSkip(true);
						flag=field6;
						break rule;
					}
					infor.setField6(field6.trim());
					break;
				case 6:
					String field7 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
					if (StringUtils.isBlank(field7)) {
						page.setSkip(true);
						flag=field7;
						break rule;
					}
					infor.setField7(field7.trim());
					break;
				case 7:
					String field8 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
					if (StringUtils.isBlank(field8)) {
						page.setSkip(true);
						flag=field8;
						break rule;
					}
					infor.setField8(field8.trim());
					break;

				default:
					System.out.println("字段用完了！");
					break;
				}
			}
			
			if (StringUtils.isNotBlank(flag)) {
				page.putField("infos", infor);
			}
		}
	}


	/**
	 * 返回一个格式良好的日期字符串
	 * @param field3
	 * @return
	 */
	private String formatToDate(String field3) {
		String reg = "([0-9]{4})[-/\\.年]?([0-1]?[0-9]{1})[-/\\.月]?([0-3]?[0-9]{1})[日]?";//日期正则表达式
		Pattern pattern = Pattern.compile (reg);
		Matcher matcher = pattern.matcher (field3);//使用正则表达式判断日期
		if (matcher.find ()) {
			return matcher.group ();
		}else {
			return field3;
		}
	}


	@Override
	public Site getSite() {
		return site;
	}
	
}
